/**
 * MoteAppMousepadActivity.java
 *
 * Created by Andrew Moss on 16 May 2011
 *
 */
package com.mossdev.android.moteapp;

import com.mossdev.android.moteapp.comms.Commands;
import com.mossdev.android.moteapp.comms.ServerConnectionException;
import com.mossdev.android.moteapp.listeners.ButtonOnClickListener;
import com.mossdev.android.moteapp.listeners.ButtonOnTouchListener;
import com.mossdev.android.moteapp.listeners.MousepadTouchListener;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ToggleButton;

/**
 * @author Andrew Moss
 *
 */
public class MoteAppMousepadActivity extends Activity {
	
	private ToggleButton tbutton;
	
	private OnClickListener toggle_mote_listen = new OnClickListener() {
	    public void onClick(View v) {
	        // Perform action on clicks
	        if (tbutton.isChecked()) {
				try {
					MoteApp.servercomms.sendCommand(Commands.MOUSE_DOWN_L);
				} catch (ServerConnectionException e) {
					Log.e(Config.LogTag, e.err_msg);
				}
	        } else {
	        	 try {
	   	    	  MoteApp.servercomms.sendCommand(Commands.MOUSE_UP_L);
	   	      } catch (ServerConnectionException e) {
	   	    	  Log.e(Config.LogTag, e.err_msg);
	   	      }
	        }
	    }
	};
	
	//Constructor
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mousepad);
        
        //Setup pointer buttons
        this.tbutton = (ToggleButton)findViewById(R.id.mousebutton1);
        tbutton.setOnClickListener(this.toggle_mote_listen);
        
        Button button = (Button)findViewById(R.id.mousebutton2);
        button.setOnClickListener(new ButtonOnClickListener(Commands.MOUSE_CLICK_M));
        button = (Button)findViewById(R.id.mousebutton3);
        button.setOnClickListener(new ButtonOnClickListener(Commands.MOUSE_CLICK_R));
        
        //Setup the touchpad
        View touchview = findViewById(R.id.TouchView);
        touchview.setOnTouchListener(new MousepadTouchListener());
    }

}
